#!/bin/bash

function gpio_print() {
GPIOBASE=(`ls /sys/class/gpio`)
echo ${GPIOBASE[*]}
i=0
while [ ${GPIOBASE[${i}]} ]
do
  GPIOCHIP=${GPIOBASE[${i}]#*gpiochip}
  if [ $GPIOCHIP = 'export' -o $GPIOCHIP = 'unexport' ]
  then
    i=$((i+1))
    continue
  fi

  BASE=$(cat /sys/class/gpio/gpiochip${GPIOCHIP}/base)
  NGPIO=$(cat /sys/class/gpio/gpiochip${GPIOCHIP}/ngpio)
  max=$((${BASE}+${NGPIO}))
  gpio=$BASE
  while [ $gpio -lt $max ]
  do
    echo $gpio > /sys/class/gpio/export
    [ -d /sys/class/gpio/gpio${gpio} ] && {
      echo in > /sys/class/gpio/gpio${gpio}/direction
      echo "[GPIO${gpio}] value: $(cat /sys/class/gpio/gpio${gpio}/value)"
      echo ${gpio} > /sys/class/gpio/unexport
    }
    gpio=$((gpio+1))
  done
  i=$((i+1))
done
}

gpio_print
